package com.ruoyi.js.controller;

import java.io.IOException;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import com.ruoyi.common.config.Global;
import com.ruoyi.common.constant.RoleConstants;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.js.utils.RandomUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.js.domain.JsMatch;
import com.ruoyi.js.service.IJsMatchService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 比赛Controller
 * 
 * @author greejoy
 * @date 2024-04-22
 */
@Controller
@RequestMapping("/js/match")
public class JsMatchController extends BaseController
{
    private String prefix = "js/match";

    @Autowired
    private IJsMatchService jsMatchService;

    @RequiresPermissions("js:match:view")
    @GetMapping()
    public String match()
    {
        return prefix + "/match";
    }

    /**
     * 查询比赛列表
     */
    @RequiresPermissions("js:match:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(JsMatch jsMatch)
    {
        startPage();
        List<JsMatch> list = jsMatchService.selectJsMatchList(jsMatch);
        return getDataTable(list);
    }

    /**
     * 导出比赛列表
     */
    @RequiresPermissions("js:match:export")
    @Log(title = "比赛", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(JsMatch jsMatch)
    {
        List<JsMatch> list = jsMatchService.selectJsMatchList(jsMatch);
        ExcelUtil<JsMatch> util = new ExcelUtil<JsMatch>(JsMatch.class);
        return util.exportExcel(list, "match");
    }

    /**
     * 新增比赛
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存比赛
     */
    @RequiresPermissions("js:match:add")
    @Log(title = "比赛", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(JsMatch jsMatch, MultipartFile screenFile)
    {
        // 同时只支持一场比赛正在打分
        if(jsMatch.getMatchScore()){
            JsMatch q_m = new JsMatch();
            q_m.setMatchScore(true);
            List<JsMatch> matchList = jsMatchService.selectJsMatchList(q_m);
            matchList.forEach(match -> {
                match.setMatchScore(false);
                jsMatchService.updateJsMatch(match);
            });
        }
        buildMatchFiles(jsMatch, screenFile);
        return toAjax(jsMatchService.insertJsMatch(jsMatch));
    }

    /**
     * 修改比赛
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        JsMatch jsMatch = jsMatchService.selectJsMatchById(id);
        mmap.put("jsMatch", jsMatch);
        return prefix + "/edit";
    }

    /**
     * 修改保存比赛
     */
    @RequiresPermissions("js:match:edit")
    @Log(title = "比赛", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(JsMatch jsMatch, MultipartFile screenFile)
    {
        // 同时只支持一场比赛正在打分
        if(jsMatch.getMatchScore()){
            JsMatch q_m = new JsMatch();
            q_m.setMatchScore(true);
            List<JsMatch> matchList = jsMatchService.selectJsMatchList(q_m);
            matchList.forEach(match -> {
                match.setMatchScore(false);
                jsMatchService.updateJsMatch(match);
            });
        }
        buildMatchFiles(jsMatch, screenFile);
        return toAjax(jsMatchService.updateJsMatch(jsMatch));
    }

    /**
     * 删除比赛
     */
    @RequiresPermissions("js:match:remove")
    @Log(title = "比赛", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(jsMatchService.deleteJsMatchByIds(ids));
    }

    /**
     * 获取全部比赛
     */
    @GetMapping("/getAllMatch")
    @ResponseBody
    public AjaxResult getAllMatch()
    {
        List<JsMatch> list = jsMatchService.selectJsMatchList(null);
        return AjaxResult.success(list);
    }

    /**
     * 获取比赛信息
     */
    @GetMapping("/getMatch")
    @ResponseBody
    public AjaxResult getMatch(Long id)
    {
        JsMatch match = jsMatchService.selectJsMatchById(id);
        return AjaxResult.success(match);
    }

    private void buildMatchFiles(JsMatch match, MultipartFile screenFile){
        // 上传文件路径
        String filePath = Global.getUploadPath();
        if(screenFile != null){
            String screen = null;
            try {
                screen = FileUploadUtils.upload(filePath, screenFile);
            } catch (IOException e) {
                e.printStackTrace();
            }
            match.setScreenImage(screen);
        }
    }

}
